package com.sun.designpatternsdemo.command

import android.util.Log
import android.widget.Toast
import com.sun.designpatternsdemo.App

/**
 * 版权：xx公司 版权所有
 * @author sun
 * 版本：1.0
 * 创建日期：2021/6/30 9:43
 * 描述：接口
 *
 */
interface Command {
    fun execute()
}

class Light {
    fun on() {
        Log.e("Light:", "---打开灯")
    }

    fun off() {
        Log.e("Light:", "---关闭灯")
    }
}

class Door {
    fun open() {
        Log.e("Door:", "---打开门")
    }

    fun close() {
        Log.e("Door:", "---关闭门")
    }
}

class Computer {
    fun on() {
        Log.e("Computer:", "---打开电脑")
    }

    fun off() {
        Log.e("Computer:", "---关闭电脑")
    }
}


class ComputerOnCommand(var computer: Computer): Command{
    override fun execute() {
        computer.on()
    }
}
class ComputerOffCommand(var computer: Computer): Command{
    override fun execute() {
        computer.off()
    }
}

class DoorCloseCommand(var door: Door):Command{
    override fun execute() {
        door.close()
    }
}

class DoorOpenCommand(var door: Door):Command{
    override fun execute() {
        door.open()
    }
}

class LightOnCommand(var light: Light):Command{
    override fun execute() {
       light.on()
    }
}
class LightOffCommand(var light: Light):Command{
    override fun execute() {
        light.off()
    }
}

class NoCommand:Command{
    override fun execute() {
        Toast.makeText(App.instance, "哥们,这个没效!", Toast.LENGTH_SHORT).show()
    }
}
class QuickCommand(private val commands: Array<Command>) : Command {
    override fun execute() {
        for (command in commands) {
            command.execute()
        }
    }
}

class ControlPanel {
    private val commands: Array<Command?> = arrayOfNulls(CONTROL_SIZE)

    /**
     * 设置每个按钮对应的位置
     */
    fun setCommands(index: Int, command: Command?) {
        commands[index] = command
    }

    /**
     * 模拟点击按钮
     */
    fun keyPressed(index: Int) {
        commands[index]!!.execute()
    }

    companion object {
        private const val CONTROL_SIZE = 10
    }

    init {
        for (i in 1 until CONTROL_SIZE) {
            commands[i] = NoCommand()
        }
    }
}
